<template>
  <div class="unpaid-bill-management">
    <div style="margin-bottom: 15px;">
      <h3>未缴账单</h3>
    </div>

    <!-- 操作栏：按钮 + 搜索 -->
    <div class="operation-bar">
      <el-button type="primary" @click="printBills">打印收费通知单</el-button>
      <el-button type="success" style="margin-left: 10px;" @click="sendSMS">发短信</el-button>
      <el-button type="success" style="margin-left: 10px;" @click="sendWeChat">发微信</el-button>
      <el-button type="danger" style="margin-left: 10px;" @click="deleteSelectedBills">删除选中账单</el-button>
      <div class="search-group" style="margin-left: auto;">
        <el-input v-model="searchKeyword" placeholder="小区/房号/收费项目/收费标准" style="width: 240px;" />
        <el-button type="primary" icon="el-icon-search" @click="searchBills"></el-button>
        <el-button type="primary" icon="el-icon-filter" @click="filterBills"></el-button>
      </div>
    </div>
    <!-- 未缴账单表格 -->
    <el-table :data="tableData" border style="width: 100%; margin-bottom: 20px;"
      @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" />
      <el-table-column prop="communityName" label="小区" />
      <el-table-column prop="number" label="房号/车位号/车牌号/住户" width="180" />
      <el-table-column prop="clientName" label="住户姓名" />
      <el-table-column prop="clientPhone" label="手机号" />
      <el-table-column prop="pricingName" label="费用名称" />
      <el-table-column prop="settingPrice" label="单价" />
      <el-table-column prop="billNumber" label="数量" />
      <el-table-column prop="billSum" label="应收金额" />
      <el-table-column prop="settingPrice" label="实收金额" />
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="viewBill(scope.row)">查看</el-button>
          <el-button type="danger" size="small" text @click="deleteBill(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <div class="pagination-wrap">
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage"
        :page-sizes="[10, 20, 30, 40]" :page-size="pageSize" layout="prev, pager, next, ->, jumper, ->, sizes, total"
        :total="total" />
    </div>
  </div>
</template>

<script>
import { billApi } from '@/api/bill'
import request from '@/utils/request';

export default {
  name: 'UnpaidBillManagement',
  data() {
    return {
      currentPage: 1,
      pageSize: 10,
      total: 0,
      tableData: [],
      selectedRows: [],
      searchKeyword: '',
      selectedReceivable: 0,
      selectedDiscount: 0,
      selectedOverdueFine: 0,
      selectedPayable: 0
    }
  },
  created() {
    this.fetchBills();
  },
  methods: {
    async fetchBills() {
      try {
        const params = {
          page: this.currentPage,
          size: this.pageSize,
          billStatus: 0,
        };

        const response = await billApi.getBillByStatus(params);
        if (response.code === 200) {
          console.log('获取账单数据成功:', response);
          this.tableData = response.data.records;
          this.total = response.data.total;
        } else {
          console.error('获取账单数据失败:', response.message);
          this.$message.error(response.message || '获取账单数据失败');
        }
      } catch (error) {
        console.error('请求账单数据时发生错误:', error);
        this.$message.error('获取账单数据失败');
      }
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.fetchBills();
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.fetchBills();
    },
    handleSelectionChange(val) {
      this.selectedRows = val;
      this.selectedReceivable = this.selectedRows.reduce((sum, row) => sum + row.receivable, 0);
      this.selectedDiscount = this.selectedRows.reduce((sum, row) => sum + row.discount, 0);
      this.selectedOverdueFine = this.selectedRows.reduce((sum, row) => sum + row.overdueFine, 0);
      this.selectedPayable = this.selectedRows.reduce((sum, row) => sum + row.payable, 0);
    },
    printBills() {
      // 打印选中账单逻辑
      console.log('打印选中账单:', this.selectedRows);
    },
    sendSMS() {
      // 发送短信逻辑
      console.log('发送短信给选中账单:', this.selectedRows);
    },
    sendWeChat() {
      // 发送微信逻辑
      console.log('发送微信给选中账单:', this.selectedRows);
    },
    deleteSelectedBills() {
      // 删除选中账单逻辑
      console.log('删除选中账单:', this.selectedRows);
    },
    searchBills() {
      // 搜索账单逻辑
      console.log('搜索账单:', this.searchKeyword);
    },
    filterBills() {
      // 过滤账单逻辑
    },
    viewBill(row) {
      // 查看账单逻辑
      console.log('查看账单:', row);
    },
    deleteBill(row) {
      // 删除账单逻辑
      console.log('删除账单:', row);
    }
  }
}
</script>

<style scoped>
.unpaid-bill-management {
  padding: 20px;
}

.function-desc {
  font-size: 14px;
  color: #606266;
  margin-bottom: 15px;
}

.operation-bar {
  display: flex;
  align-items: center;
  margin-bottom: 15px;
}

.search-group {
  display: flex;
  align-items: center;
}

.search-group .el-input {
  margin-right: 8px;
}

.pagination-wrap {
  display: flex;
  justify-content: flex-end;
}
</style>
